package org.utils.lang;

import java.util.Timer;
import java.util.TimerTask;

/**
 * @author Manuel EVENO
 */
public class GarbageCollectorTask extends TimerTask
{
	/** DEFAULT_DELAY = 5 minutes */
	public final static int DEFAULT_DELAY = 1000*60*5;
	protected static Timer timer = null;

	/**
	 * Schedule GarbageCollectorTask with the DEFAULT_DELAY
	 */
	public static void schedule()
	{
		schedule(DEFAULT_DELAY);
	}
	/**
	 * Schedule GarbageCollectorTask with the specified delay
	 * @param delay the period to launch the task
	 */
	public static void schedule(int delay)
	{
		timer = new Timer();
		timer.schedule(new GarbageCollectorTask(), delay, delay);
	}
	/**
	 * Cancels the timer and forget the reference
	 */
	public static void stop()
	{
		timer.cancel();
		timer = null;
	}
	/**
	 * Constructor for GarbageCollectorTask.
	 */
	public GarbageCollectorTask()
	{
		super();
	}
	/**
	 * Run the garbage collection and the finalization
	 * @see java.lang.System#gc()
	 * @see java.lang.System#runFinalization()
	 * @see java.lang.Runnable#run()
	 */
	public void run()
	{
		System.gc();
		System.runFinalization();
	}
}
